package com.gzgy.ustore.api.service.impl;

import com.gzgy.ustore.api.common.exception.BaseAPIException;
import com.gzgy.ustore.api.common.exception.ExceptionCodeStatusEnum;
import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.dao.PdaMgrDao;
import com.gzgy.ustore.api.model.PdaMgrList;
import com.gzgy.ustore.api.service.PdaMgrService;
import com.ustore.model.PdaMgr;
import org.apache.xmlbeans.impl.piccolo.util.DuplicateKeyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PdaMgrServiceImpl implements PdaMgrService {

    @Autowired
    private PdaMgrDao pdaMgrDao;

    @Override
    public String registerPda(PdaMgr pdaMgr) {
       int count = pdaMgrDao.getPdaMgrBySn(pdaMgr.getDvcId());
       if(count>0){
           int errorCode = ExceptionCodeStatusEnum.DVC_HAVE_BEEN_REGISTER_EXCEPTION.toInt();
           String errorMsg = I18nUtil.getMessage(errorCode,null,"zh_CN");
           throw new BaseAPIException(errorMsg,errorCode);
       }
       return pdaMgrDao.add(pdaMgr).getId().toString();
    }

    @Override
    public String updateDeviceInfo(PdaMgr pdaMgr, String flag) throws SQLException {

        PdaMgr pdaMgr1 = pdaMgrDao.get(pdaMgr.getId());
        if (pdaMgr1 == null) {
            int errorCode = ExceptionCodeStatusEnum.DVC_NOT_FOUND_EXCEPTION.toInt();
            String errorMsg = I18nUtil.getMessage(-2, null, "zh_CN");
            throw new BaseAPIException(errorMsg, errorCode);
        }
        // 0 UPDATE ALL DATE
        // 1 ONLY UPDATE STATUS
        if ("0".equals(flag)) {
            pdaMgr1.setRemark(pdaMgr.getRemark());
            pdaMgr1.setDvcGroupId(pdaMgr.getDvcGroupId());
            pdaMgr1.setDvcType(pdaMgr.getDvcType());
            pdaMgr1.setOrgTreeId(pdaMgr.getOrgTreeId());
            pdaMgr1.setStatus(pdaMgr.getStatus());
            pdaMgr1.setOrgName(pdaMgr.getOrgName());
        } else {
            if (PdaMgr.DVC_STATUS.CANCEL.getValue().equals(pdaMgr.getStatus().toString())) {
                pdaMgrDao.del(pdaMgr.getId());
            } else {
                pdaMgr1.setStatus(pdaMgr.getStatus());
            }
        }
        pdaMgrDao.update(pdaMgr1);

        return "操作成功";
    }

    @Override
    public Map<String, Object> pdaList(String companyId, String orgId, String dvcGroupId, String index, String page) {
       Map<String,Object> map = new HashMap<String,Object>();
       //获取总数量
        int count = 0;
        count = pdaMgrDao.countPdaList(companyId, orgId, dvcGroupId);
        List<PdaMgrList> list = new ArrayList<PdaMgrList>();
        if (count > 0) {
            list = pdaMgrDao.PdaList(companyId,orgId,dvcGroupId,index,page);
        }
        map.put("dvcList",list);
        map.put("count",count);
        map.put("index",index);

        return map;
    }
}
